<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/12/7
 * Time: 11:09
 */

namespace app\zyhsapi\controller\v1;

use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsAstrTasks;
use app\zyhsapi\model\ZyhsTag;
use app\zyhsapi\model\ZyhsUserAppeal;
use app\zyhsapi\model\ZyhsUserQuestion;
use app\zyhsapi\model\ZyhsUserQuestionComment;
use app\zyhsapi\model\ZyhsUserReport;
use think\response\Json;

class Complain extends ZyhsAuth
{
    /**
     * 获取申述标签
     * @return Json
     * @throws \Exception
     */
    public function getAppealTag()
    {
        $data = array_values((new ZyhsTag())->getTag(3));
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 获取举报原因
     * @return Json
     * @throws \Exception
     */
    public function getReportTag()
    {
        $data = array_values((new ZyhsTag())->getTag(2));
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 申诉
     * @return \think\response\Json|void
     * @throws \Exception
     */
    public function appeal()
    {
        $taskId = input('task_id/d', 0);
        $id = input('id/d', 0);
        $tel = input('tel/s', '');
        $reason = input('reason/s', '');
        if (!$tel)
        {
            return show(config('code.error'), '手机号不能为空');
        }
        if (!$reason)
        {
            return show(config('code.error'), '申诉原因不能为空');
        }
        $appeaTag = (new ZyhsTag())->getTag(3);
        if (empty($appeaTag[$id]))
        {
            return show(config('code.error'), '申述标签不存在');
        }
        $zyhsAstrTasksModel = new ZyhsAstrTasks();
        $task = $zyhsAstrTasksModel->getInfoById($taskId);
        if (empty($task) || $task['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '任务不存在');
        }
        if ($task['complain_status'] === 1)
        {
            return show(config('code.error'), '任务已申诉');
        }
        $zyhsAstrTasksModel->UpdateData($taskId, $task['astr_id'], ['complain_status' => 1, 'update_time' => $this->request->time()]);
        (new ZyhsUserAppeal())->insert([
            'type' => 1,
            'data_id' => $taskId,
            'contact' => $tel,
            'reason' => $reason,
            'tid' => $id,
            'uid' => $this->user['id'],
            'astr_id' => $task['astr_id'],
            'status' => 0,
            'create_time' => $this->request->time(),
        ]);
        return show(config('code.success'), '申诉成功');
    }

    /**
     * 举报
     * @return Json|void
     * @throws \Exception
     */
    public function report()
    {
        $qid = input('qid/d', 0);
        $id = input('id/d', 0);
        $appeaTag = (new ZyhsTag())->getTag(2);
        if (empty($appeaTag[$id]))
        {
            return show(config('code.error'), '举报标签不存在');
        }
        $question = (new ZyhsUserQuestion())->getInfoById($qid);
        if (empty($question))
        {
            return show(config('code.error'), '问题不存在');
        }
        (new ZyhsUserReport())->insert([
            'type' => 1,
            'data_id' => $qid,
            'tid' => $id,
            'uid' => $this->user['id'] ?? '',
            'create_time' => $this->request->time(),
        ]);
        return show(config('code.success'), '举报成功');
    }

    /**
     * 获取评价标签
     * @return Json
     * @throws \Exception
     */
    public function getCommentTag()
    {
        $tagModel = new ZyhsTag();
        $tag = $tagModel->getLevelTag(4, 0);
        $tag = $tagModel->getQuestionAllsResrt($tag);
        return show(config('code.success'), '请求成功', $tag);
    }

    /**
     * 评价
     * @return Json
     * @throws \Exception
     */
    public function comment()
    {
        $taskId = input('task_id/d', 0);
        $level = input('level/d', 0);
        $remark = input('remark/s', '');
        $tids = input('tids/s', '');

        if (mb_strlen($remark) > 60)
        {
            return show(config('code.error'), '评价内容不能超过60个字符');
        }
        $taskInfo = (new ZyhsAstrTasks())->getInfoById($taskId);
        if (empty($taskInfo) || $taskInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '任务不存在');
        }
        if ($taskInfo['is_comment'] > 0)
        {
            return show(config('code.error'), '服务异常，已经评价过了～');
        }
        $tidsArr = explode(',', $tids);
        $tags = (new ZyhsTag())->batchGetInfoByIds($tidsArr, ['flag', 'tag_type']);
        $flags = array_unique(array_column($tags, 'flag'));
        $flag = (int)current($flags);
        if ($flag !== $level || count($flags) > 1)
        {
            return show(config('code.error'), '评价标签异常');
        }
        //问答标签类型
        if ((int)current($tags)['tag_type'] !== 4)
        {
            return show(config('code.error'), '评价标签异常');
        }

        if ($taskInfo['status'] === 6 && $level !== 3)
        {   // 已完成 评价更新 能力分数
            $commentLevelChangeScoreData = [
                1 => -20,
                2 => -10,
                3 => 0,
                4 => 3,
                5 => 5,
            ];
            $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
            $consultAccountInfo = $zlcr3ConsultAccountModel->getInfoById($taskInfo['astr_id']);
            // 更新能力分
            $zlcr3ConsultAccountModel->changeAstrLevelScore($consultAccountInfo, $taskId, 2, $commentLevelChangeScoreData[$level], 1);
        }

        (new ZyhsUserQuestionComment())->saveData([
            'uid' => $this->user['id'],
            'task_id' => $taskId,
            'level' => $level,
            'tids' => $tids,
            'comment' => $remark,
            'astr_id' => $taskInfo['astr_id'],
            'create_time' => $this->request->time(),
        ]);

        (new ZyhsAstrTasks())->UpdateData($taskId, $taskInfo['astr_id'], [
            'is_comment' => 1,
        ]);
        return show(config('code.success'), 'Success.');
    }

    /**
     * 删除评价
     * @return Json
     * @throws \Exception
     */
    public function delComment()
    {
        $taskId = input('task_id/d', 0);
        $comment = (new ZyhsUserQuestionComment())->getInfoById($taskId, ['uid', 'astr_id']);
        if (empty($comment))
        {
            return show(config('code.error'), '评论不存在');
        }
        if ($comment['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '你没有权限删除该评论');
        }

        (new ZyhsUserQuestionComment())->updateData($taskId, $comment['astr_id'], [
            'delete_time' => $this->request->time(),
        ]);
        return show(config('code.success'), 'Success.');
    }
}
